<?php
require_once 'config.php'; 
require_once 'ServerCache.class.php'; 
require_once 'Fetion.class.php'; 
class Tools{

    //中文分词
    public static function Segment($str){
        $seg = new SaeSegment();
        $ret = $seg->segment($str, 1);
        return $ret;
    }

	//发送飞信
	public static function SendMMS($phone,$msg){
        $sj=F_SJ;
        $mm=F_MM;
		$fx = new PHPFetion($sj,$mm);
		$fx->send($phone,$msg);
	}

    //用于请求DZU_URL。便于数据缓存，参数过滤以及加密签名
    public static function getHttp($url){
        $r=ServerCache::getCache($url);//获取缓存
        if($r=='no'){
            $f=strpos($url, '?')?'&':'?';
            $u=$url.$f.'xn='.XN.'&xq='.XQ;
            $u.='&sign='.Tools::makeSign($u);
            $r=file_get_contents($u);
            mydebug($u);
            ServerCache::setCache($url,$r);//设置缓存
        }
        return $r;
    }

    //生成签名
    public static function makeSign($url){
        $p=str_replace(DZU_URL, "", $url);
        $sign=md5($p.SIGN_KEY.Tools::timeKey());//请求+密钥+时间有关的key
        return $sign;
    }
    //时间key，用于时间限，制校正接口服务器时间
    public static function timeKey(){
        //签名有效时时间为10分钟
        return date("YmdH").floor(date("i")/10);;
    }

	//提取信息
    public static function getInfo($info,$c){
        $r="null";
        $a=explode("#", $info);
        for($i=1 ;$i<count($a);$i++){
            if(substr($a[$i], 0,strlen($c))==$c){
                $r=str_replace($c.":","", $a[$i]);
                $r=trim($r);
                if($r=='NULL'||$r==''){
                   $r='null'; 
                } else{
                  return $r;  
                } 
            }
        }
        return $r;
    }

    //记录用户发送的日志
    public static function wxLog($openid,$keyword,$act){
        $sql = "INSERT INTO `smartdzu_wxlog` (`oid`, `action`, `keyword`, `date`) VALUES ('$openid', '$act','$keyword', NOW());";
        mysql_query($sql);
    }

    //教务系统密码解密
    public static function JWMM($PlainStr,$key="Encrypt01"){
        $Pos = 1;
        if(strlen($PlainStr)%2 == 0){
            $Side1 = substr($PlainStr,0,strlen($PlainStr)/2);
            $Side1=implode(array_reverse(str_split($Side1,1)));
            $Side2 = substr($PlainStr,strlen($PlainStr)/2);
            $Side2=implode(array_reverse(str_split($Side2,1)));
            $PlainStr = $Side1.$Side2;
        }
        $_Vb_t_i4_0 = strlen($PlainStr);
        $bl_1=0;
        $bl_2=0;
        $bl_3=0;
        $bl_4=0;
        for($i=1; $i<=$_Vb_t_i4_0; $i++){
            $strChar = substr($PlainStr,$i-1, 1);
            $KeyChar = substr($key,$Pos-1, 1);

            $bl_1 = (ord($strChar ^ $KeyChar)) < 32? 1:0;
            $bl_2 = (ord($strChar ^ $KeyChar)) > 126? 1:0;
            $bl_3 = (ord($strChar) < 0? 1:0) | ($bl_1 | $bl_2);
            $bl_4 = (ord($strChar) > 0xFF? 1:0) | $bl_3;
            if($bl_4){
                $NewStr .= $strChar;        
            }else{
                $ch = $strChar ^ $KeyChar;
                $str = "";
                $str .= $ch;
                $NewStr .= $str;
            }
            if(strlen($key) == $Pos){
                $Pos = 0;
            }
            $Pos += 1;
        }
        return $NewStr;
    }

}
?>